package com.yzpc.controller;

import com.yzpc.entity.MouseClick;
import com.yzpc.entity.Script;
import com.yzpc.service.IScriptService;
import com.yzpc.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
@RequestMapping("/script")
public class ScriptController {
    @Autowired
    private IScriptService scriptService;

    @GetMapping("/{scriptId}")
    @ResponseBody
    public R getInfo(@PathVariable Integer scriptId) {

        return R.success(null, scriptService.findScriptById(scriptId));
    }
    @GetMapping("/name/{userName}")
    @ResponseBody
    public R getInfo(@PathVariable String userName) {
        return R.success(null, scriptService.findScriptByName(userName));
    }
    @GetMapping("/list")
    @ResponseBody
    public R list(Script script) {
        return R.success(null, scriptService.findScriptList(script));
    }

    @PostMapping("/add")
    @ResponseBody
    public R add(@RequestBody Script script) {
        int rtn = scriptService.insertScript(script);
        if(rtn > 0) {
            return R.success();
        } else {
            return R.error();
        }
    }
    @PutMapping("/edit")
    @ResponseBody
    public R edit(@RequestBody Script script) {
        int rtn = scriptService.updateScript(script);
        if(rtn > 0) {
            return R.success();
        } else {
            return R.error();
        }
    }
    @DeleteMapping("/{scriptId}")
    @ResponseBody
    public R delete(@PathVariable Integer scriptId) {
        scriptService.deleteScriptById(scriptId);
        return R.success();
    }

//导入脚本
    @GetMapping("/getMousePositions")
    @ResponseBody
    public List<MouseClick> getMousePositions() {
        System.out.println(scriptService.getMouseClicks());
        return scriptService.getMouseClicks();
    }

    @PostMapping("/saveActions")
    public void saveActions(@RequestBody SaveActionsRequest request) {
        scriptService.saveScript(request.getScriptName(), request.getActions(), request.getUserName());
    }

    public static class SaveActionsRequest {
        private String scriptName;
        private List<MouseClick> actions;
        private String userName; // 接收前端传来的 userName

        public String getScriptName() {
            return scriptName;
        }

        public void setScriptName(String scriptName) {
            this.scriptName = scriptName;
        }

        public List<MouseClick> getActions() {
            return actions;
        }

        public void setActions(List<MouseClick> actions) {
            this.actions = actions;
        }

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }
    }
//    启动脚本
    @PostMapping("/start/{scriptId}")
    @ResponseBody
    public R startScript(@PathVariable Integer scriptId) {
        int result = scriptService.updateScriptUsed(scriptId, 1); // 将 used 更新为 1
        if(result > 0) {
            return R.success();
        } else {
            return R.error();
        }
    }
}
